/*
 * GroupController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.group, Version 1.0 (the "License");
 *
 */
package org.geek.group.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.common.base.BaseController;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtCheckTree;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.common.entity.ExtTree;
import org.geek.common.entity.Menu;
import org.geek.common.util.TreeUtil;
import org.geek.domain.Group;
import org.geek.domain.User;
import org.geek.group.service.IGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * </p>
 * 
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
@RequestMapping(("/group/"))
public class GroupController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(GroupController.class);

	@Resource
	private IGroupService groupService;

	@RequestMapping(value = "initPage.jhtml", method = RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		model.put("parentId", request.getParameter("parentId"));
		this.log(request, logger, "访问信息功能");
		return "group/group";
	}

	@RequestMapping(value = "getDataList.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public Object getDataList(ExtPager pager, HttpServletRequest request) {
		Criteria criteria = new Criteria();
		List<Group> groups = groupService.getGroupByCriteria(criteria);//取出所有的
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			criteria.put("id", id);
		}
		int total = groupService.countGroupByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		List<Group> list = groupService.getGroupByCriteria(criteria);
		for(Group group :groups){
			for(Group g: list){
				if(group.getGroupId()==g.getParentId()){
					g.setParentName(group.getGroupName());
				}
			}
		}
		return new ExtGridReturn(total, list);
	}

	@RequestMapping(value = "getGroupTree.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public List<ExtTree> getGroupTree(HttpServletRequest request) {
		String groupId = request.getParameter("id");
		List<Menu> list = groupService.getGroups2Menu(0);
		return TreeUtil.getTree(list, Integer.valueOf(groupId));
	}
	
	@RequestMapping(value = "getGroupCheckBoxTree.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public List<ExtCheckTree> getGroupCheckBoxTree(HttpServletRequest request) {
		String groupId = request.getParameter("id");
		List<Menu> list = groupService.getGroups2Menu(0);
		User user = this.getUser(request);
		List<Menu> menuHasAuth = groupService.getGroups2Menu(user.getUserId());
		return TreeUtil.getTreeHasCheckBox(list, Integer.valueOf(groupId), menuHasAuth);
	}

	@RequestMapping(value = "addGroup.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public Object addGroup(HttpServletRequest request, Group group) {
		boolean success = true;

		if (StringUtils.isEmpty(group.getGroupName()))
			success = false;

		if (success && groupService.addGroup(group) <= 0) {
			success = false;
		}
		String msg = success ? "组信息添加成功！" : "组信息添加失败！";

		log(request, logger, msg);
		return new ExtReturn(success, msg);
	}

	@RequestMapping(value = "modGroup.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public Object modGroup(HttpServletRequest request, Group group) {
		boolean success = true;

		if (StringUtils.isEmpty(group.getGroupName()))
			success = false;

		if (success && groupService.modGroup(group) <= 0) {
			success = false;
		}
		String msg = success ? "组信息修改成功！" : "组信息修改失败！";
		log(request, logger, msg);
		return new ExtReturn(success, msg);
	}

	@RequestMapping(value = "delGroupByGroupId.jhtml", method = RequestMethod.POST)
	@ResponseBody
	public Object delGroupByGroupId(HttpServletRequest request, int id) {
		boolean success = true;
		if (groupService.delGroupByGroupId(id) <= 0) {
			success = false;
		}
		String msg = success ? "组信息删除成功！" : "组信息删除失败！";
		log(request, logger, msg);
		return new ExtReturn(success, msg);
	}
}
